package com.jic.product.controller.manage;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.common.util.BeanUtil;
import com.jic.product.manage.request.BrandManagementRequest;
import com.jic.product.manage.response.BrandManagementResponse;
import com.jic.product.manage.response.ProductBrandResponse;
import com.jic.product.service.BrandManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by HeJun on 2020/1/11
 */

@Api(value="商家品牌资质管理controller", tags = "商家品牌资质管理", description = "BrandManagementController")
@RestController
@RequestMapping("/brandManagement")
public class BrandManagementController{

    @Autowired
    BrandManagementService brandManagementService;

    @ApiOperation(value="获取品牌资质信息", notes="获取品牌资质信息")
    @PostMapping("/queryPage")
    public RestResult<PageResult<BrandManagementResponse>> queryPage(@RequestBody BrandManagementRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());

        BrandManagementRequest brandManagement = BeanUtil.copy(form, BrandManagementRequest.class);
        PageResult<BrandManagementResponse> result = brandManagementService.queryPage(brandManagement, page);

        List<BrandManagementResponse> brandManagementResponses = result.getRows().stream().map(temp -> {
            BrandManagementResponse bmr = BeanUtil.copy(temp, BrandManagementResponse.class);
            return bmr;
        }).filter(item -> item != null).collect(Collectors.toList());

        PageResult<BrandManagementResponse> pageResult = new PageResult<>();
        pageResult.setRows(brandManagementResponses);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }


    @ApiOperation(value="获取商家资质品牌", notes="获取商家资质品牌")
    @PostMapping("/queryMerchantProductBrand")
    public RestResult<List<ProductBrandResponse>> queryMerchantProductBrand(@RequestBody BrandManagementRequest form) {
        return RestResult.success(brandManagementService.queryMerchantProductBrand(form));
    }


    @ApiOperation(value = "申请品牌资质", notes = "申请品牌资质")
    @PostMapping("/insert")
    public RestResult<Long> insert(@RequestBody BrandManagementRequest t) {
        long result = 0;
        try {
            t.setDeptId(t.getAdminOperate().getDeptId());
            RestResult resultInfo=brandManagementService.queryInfo(t);
            if (null ==resultInfo.getData()){
                result = brandManagementService.insert(t);
            }else{
                throw new Exception("该品牌已存在!请检查！");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return RestResult.error("9999", e.getLocalizedMessage());
        }
        return RestResult.success(result);
    }

    @ApiOperation(value = "审核品牌资质信息", notes = "审核品牌资质信息")
    @PostMapping("/update")
    public RestResult<Long> update(@RequestBody BrandManagementRequest t) {
        long result = 0;
        if (StringUtils.isNotEmpty(t.getIds())) {
            String[] ids = t.getIds().split(",");
            for (int i = 0; i < ids.length; i++) {
                t.setId(Long.parseLong(ids[i]));
                try {
                    result = brandManagementService.update(t);
                } catch (Exception e) {
                    e.printStackTrace();
                    return RestResult.error("9999", e.getLocalizedMessage());
                }
            }
            return RestResult.success(result);
        }
        try {
            result = brandManagementService.update(t);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResult.error("9999", e.getLocalizedMessage());
        }
        return RestResult.success(result);
    }


    @ApiOperation(value = "删除品牌资质信息", notes = "删除品牌资质信息")
    @PostMapping("/delete")
    public RestResult<Long> delete(@RequestBody BrandManagementRequest brand) {
        long result = 0;
        try {
            if (brand !=null){
                result = brandManagementService.delete(brand.getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return RestResult.error("9999", e.getLocalizedMessage());
        }
        return RestResult.success(result);
    }

}
